class Position {
    constructor({
                    id = '',
                    positionNumber = '',
                    name = '',
                    serverNumber = '0',
                    frameId = '',
                } = {}) {
        this.id = id
        this.positionNumber = positionNumber
        this.name = name
        this.serverNumber = serverNumber
        this.frameId = frameId
    };
}

//vue
new BaseValidateVue({
    el: '#app',
    data() {
        return {
            formData: {
                houseId: '',
                houseArea: '',
                frameCode: '',
                frameName: '',
                cabinetLocation: '',
                useType: '1',
                distribution: '1',
                occupancy: '1',
                positions: [
                    new Position(),
                ],
                positionDeleteIds: [''],
            },
            areas: [],
            houses: [],
            validateList: [
                new ValidateObject({
                    ruleName: 'frame_code_validate',
                    validate: this.frameCodeValidate,
                }),
                new ValidateObject({
                    ruleName: 'position_number_validate',
                    validate: this.positionNumberValidate,
                }),
            ],
            originalFrameCode: _.cloneDeep(forward.getData().frameCode),
        }
    },
    methods: {
        createValidateList() {
            return this.validateList;
        },
        async submitForm() {
            if (!await this.$validator.validateAll()) {
                rxPrompt.dangerMsg("输入存在错误！");
                return
            }
            if (await ajaxOption({
                res: baseFrameInfoApi.update(this.formData),
                danger: '修改失败',
                success: '修改成功，即将跳转到机架列表',
            })) {
                forward('/basic-data/admin_frame_info')
            }
        },
        async initHouses() {
            this.houses = await baseHouseInfoApi.selectList()
        },
        addPositionInfo() {
            this.formData.positions.push(new Position())
        },
        async deletePositionInfo(index) {
            const res = await rxPrompt.confirm('你确定要删除吗？删除后就不能恢复了', ['确认', '取消'])
            if (!res) {
                return;
            }
            const {id = ''} = this.formData.positions[index];
            if (id != null && id !== '') {
                const res = await basePositionApi.delete(id);
                if (res.code !== 200) {
                    await rxPrompt.msg(`删除失败,${res.msg}`);
                    return;
                }
            }
            this.formData.positions.splice(index, 1);
        },
        async changeHouse() {
            this.areas = await baseFrameInfoApi.listByHouseId(this.formData.houseId)
        },
        async frameCodeValidate(val, args) {
            const {houseArea = ''} = this.formData;
            if (houseArea === '' || val === '' || val == this.originalFrameCode) {
                return true;
            }
            return await baseFrameInfoApi.selectFrameNumberByAreaId(houseArea, val);
        },
        async positionNumberValidate() {
            const positionNumbers = this.formData.positions.map(obj => obj.positionNumber);
            let flagArray = [];
            for (var i = 0; i < positionNumbers.length; i++) {
                if (flagArray[positionNumbers[i]]) {
                    return false;
                }
                flagArray[positionNumbers[i]] = true;
            }
            return true;
        },
        checkId({id = null}) {
            return id;
        }
    },
    mounted: function () {
        this.formData = _.cloneDeep(forward.getData())
        this.initHouses()
        this.changeHouse()
    },
})
